﻿module Sustainalytics.Common {

    export class Dictionary<TItem> {

        // dictionary of elements
        private _dic: any;

        // ctor.
        constructor() {
            this._dic = {};
        }

        // get dictionary keys
        public getKeys(): string[] {
            return Object.keys(this._dic);
        }

        // check whether dictionary contains a given key
        public containsKey(key: string): boolean {
            var data = this.getValue(key);
            return typeof (data) !== 'undefined';
        }

        // get dictionary value located at key
        public getValue(key: string): TItem {
            var data = this._dic[key];
            return data;
        }

        // remove dictionary key
        public removeKey(key: string): boolean {
            if (this.containsKey(key)) {
                delete this._dic[key];
                return true;
            }
            else {
                return false;
            }
        }

        // get dictionary values
        public getValues(): TItem[] {
            var values = new Array();

            for (var prop in this._dic) {
                values.push(this._dic[prop]);
            }

            return values;
        }

        public getItems(): DictionaryItem<TItem>[] {
            var items = new Array();

            for (var prop in this._dic) {
                var val = this._dic[prop];
                var dicItem = new DictionaryItem<TItem>(prop, val);
                items.push(dicItem);
            }

            return items;
        }

        // add item to dictionary
        public add(key: string, value: TItem) {

            if (this.containsKey(key))
                throw "Dictionary key already exists. Key: " + key;

            this._dic[key] = value;
        }
    }
}